var _inherit = require('core/inherit.js');
var _Container = require('component/container.js');

var _global = require('global/global.js');
var _Constant = require('scenes/game/constant.js');
var _Utility = require('scenes/utility.js');
var S = require('res/style.js');
var R = require('res/texture.js').R;
var UNIT_WIDTH = _Constant.recordSlot[0];

function createText(count) {
    var mText = S.menu.record.black.createText(count);
    mText.setAnchor(0.5, 0.5);
    mText.setPosition(0, 0);
    var width = mText.width();
    var maxwidth = UNIT_WIDTH - 16;
    if (width >= maxwidth) {
        mText.setScale(maxwidth / width, maxwidth / width);
    }
    return mText;
}

// ==========================
// ReuseScore
// ==========================
function ReuseScore() {
    _Container.call(this);

    this.m1and2 = [];
    this.mScores = [];
    this.appendText(this.m1and2, 1);
    this.appendText(this.m1and2, 2);
    var start = 3;
    while (start <= _Utility.MAX_COUNT) {
        this.appendText(this.mScores, start);
        start *= 2;
    }

    this.mSkins = [];
    for (var i = 0; i < 3; i++) {
        this.appendSkin(this.mSkins, R.cards.record_1 + i);
    }
}
_inherit(ReuseScore, _Container);
ReuseScore.prototype.appendSkin = function (arr, id) {
    var skin = _global.spriteNode(id);
    skin.setAnchor(0.5, 0.5);
    arr.push(skin);
    this.addChild(skin);
};
ReuseScore.prototype.appendText = function (arr, score) {
    var text = createText(score);
    arr.push(text);
    this.addChild(text);
};
ReuseScore.prototype.getSkinRefer = function (count) {
    if (count < 3) {
        if (count == 1) {
            return 0;
        } else {
            return 1;
        }
    } else {
        return 2;
    }
};
ReuseScore.prototype.getRefer = function (score) {
    if (score < 3) {
        return -score;
    }
    return _Utility.getScoreOrder(score);
};

ReuseScore.prototype.drawRefer = function (skin, refer, context) {
    this.mSkins[skin].draw(context);
    if (refer < 0) {
        this.m1and2[-refer - 1].draw(context);
    } else {
        this.mScores[refer].draw(context);
    }
};

module.exports = ReuseScore;